//
//  CTUserFollowCell.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/5.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTUserFollowCell.h"
#import "UIViewAdditions.h"
#import "CTHeader.h"
#import "CTAppLanguage.h"
#import "CTDebugManager.h"
#import "CTRequestUtil.h"
#import "CTUserItem.h"
#import "CTUser.h"

@interface CTUserFollowCell()
{
    UIImageView     *headImageView;
    UILabel         *nameLabel;
    UIButton        *attentionBtn;
    UILabel         *attentionLabel;
    BOOL            attentionBool;
    
    CTUserItem * followUserItem;
}
@end
@implementation CTUserFollowCell
- (void)dealloc
{
    [headImageView removeFromSuperview];
    headImageView = nil;
    [nameLabel  removeFromSuperview];
    nameLabel = nil;
    [attentionLabel removeFromSuperview];
    attentionLabel = nil;
    [attentionBtn removeFromSuperview];
    attentionBtn = nil;
    
    followUserItem = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTUserFollowCell Is Removed!!!"];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        [self awakeFromNib];
    }
    return self;
}

- (void)awakeFromNib
{
    headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 38, 38)];
    headImageView.image = [UIImage imageNamed:@"head"];
    headImageView.layer.masksToBounds = YES;
    headImageView.layer.cornerRadius  = 19;
    [self addSubview:headImageView];
    
    nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(headImageView.right+11, 26, 200, 18)];
    nameLabel.font = [UIFont systemFontOfSize:18.0f];
    nameLabel.text = @"mark";
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.textColor = CT_HEADER_BLACK_COLOR;
    [self addSubview:nameLabel];
    
    attentionLabel = [[UILabel alloc]initWithFrame:CGRectMake(CT_HEADER_SCREEN_WIDTH-60, 28, 50, 13)];
    attentionLabel.text =[CTAppLanguage localizedStringForKey:@"fllow_attention_title" withValue:nil withTable:@"Language"];
    attentionLabel.textColor = CT_HEADER_ORANGE_COLOR;
    attentionLabel.font = [UIFont systemFontOfSize:13.0];
    attentionLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:attentionLabel];
    attentionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    attentionBtn.frame = CGRectMake(CT_HEADER_SCREEN_WIDTH-60, 23, 50, 23);
    [attentionBtn addTarget:self action:@selector(attentionBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:attentionBtn];
    [attentionBtn setBackgroundImage:[UIImage imageNamed:@"bt_bg_guanzhu"] forState:UIControlStateNormal];
    
    UILabel *bottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, 67.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    bottomLine.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
    [self addSubview:bottomLine];
    
    
}
- (void)attentionBtnAction:(UIButton *) sender
{
    if (attentionBool == YES)
    {
        [attentionBtn setBackgroundImage:[UIImage imageNamed:@"guanzhu_bg"] forState:UIControlStateNormal];
        attentionLabel.text =[CTAppLanguage localizedStringForKey:@"fllow_attention_title" withValue:nil withTable:@"Language"];
        attentionLabel.textColor = CT_HEADER_GREEN_COLOR;
        attentionBool = NO;
    }
    else if (attentionBool == NO)
    {
        [attentionBtn setBackgroundImage:[UIImage imageNamed:@"guanzhu_ok_bg"] forState:UIControlStateNormal];
        attentionLabel.text = [CTAppLanguage localizedStringForKey:@"fllow_attentioned_title" withValue:nil withTable:@"Language"];
        attentionLabel.textColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
        attentionBool = YES ;
    }
    
    if ([self.delegate respondsToSelector:@selector(userFollowCellAttentionBtnAction:withItem:withBool:)])
    {
        [self.delegate userFollowCellAttentionBtnAction:self withItem:followUserItem withBool:attentionBool];
    }
    
}



#pragma mark userFollow -setter-


-(void)setUserItem:(CTUserItem *)userItem
{
    followUserItem = userItem;
    if ([userItem.uid isEqualToString:[CTUser userInfo].uid])
    {
        attentionBtn.hidden = YES;
        attentionLabel.hidden = YES;
    }
    else
    {
        attentionBtn.hidden = NO;
        attentionLabel.hidden = NO;
    }
    nameLabel.text = followUserItem.name;
    
    [CTRequestUtil requestImgWithUrl:followUserItem.profile_image_url_thumb withCompletionSuccessHandler:^(UIImage *img) {
        headImageView.image = img;
    } withFailedHandler:^(NSError *error) {
        
    }];
    
    [self setAttention:followUserItem.following];
}



- (void)setImgUrl:(NSString *)imgUrl
{
    [CTRequestUtil requestImgWithUrl:imgUrl withCompletionSuccessHandler:^(UIImage *img) {
        headImageView.image = img;
    } withFailedHandler:^(NSError *error) {
        
    }];
}

- (void)setNameStr:(NSString *)namestr
{
    nameLabel.text = namestr;
}

- (void)setAttention:(NSString *)attentionStr
{
    if ([attentionStr isEqualToString:@"1"])
    {
        [attentionBtn setBackgroundImage:[UIImage imageNamed:@"guanzhu_ok_bg"] forState:UIControlStateNormal];
        attentionLabel.text = [CTAppLanguage localizedStringForKey:@"fllow_attentioned_title" withValue:nil withTable:@"Language"];
        attentionLabel.textColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
        attentionBool = YES;
    }
    else
    {
        [attentionBtn setBackgroundImage:[UIImage imageNamed:@"guanzhu_bg"] forState:UIControlStateNormal];
        attentionLabel.text =[CTAppLanguage localizedStringForKey:@"fllow_attention_title" withValue:nil withTable:@"Language"];
        attentionLabel.textColor = CT_HEADER_GREEN_COLOR;
        attentionBool = NO;
    }
}

@end
